package myreflect5;

import java.lang.reflect.Method;

public class ReflectDemo1 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
        // method1();
        // method2();
        // method3();
        // method4();
        // method5();
    }

    private static void method5() throws ClassNotFoundException, NoSuchMethodException {
        // Method getDeclaredMethod(String name, class<?>... parameterTypes):   返回单个成员方法对象
        // 1.获取class对象
        Class<?> clazz = Class.forName("myreflect5.Student");

        // 2.获取一个成员方法show并打印
        System.out.println(clazz.getDeclaredMethod("show"));
    }

    private static void method4() throws ClassNotFoundException, NoSuchMethodException {
        // 1.获取class对象
        Class<?> clazz = Class.forName("myreflect5.Student");

        // 2.获取一个有形参的方法function2并打印
        System.out.println(clazz.getMethod("function2", String.class));
    }

    private static void method3() throws ClassNotFoundException, NoSuchMethodException {
        // Method getMethod(string name,Class<?>...parameterTypes):             返回单个公共成员方法对象
        // 1.获取class对象
        Class<?> clazz = Class.forName("myreflect5.Student");

        // 2.获取成员方法function1并打印
        System.out.println(clazz.getMethod("function1"));
    }

    private static void method2() throws ClassNotFoundException {
        // Method[] getDeclaredMethods():                                       返回所有成员圬法对象的数组，不包括继承的
        // 1.获取class对象
        Class<?> clazz = Class.forName("myreflect5.Student");
        // 2.获取Method对象
        // 3.遍历数组
        for (Method method : clazz.getDeclaredMethods()) {
            System.out.println(method);
        }
    }

    private static void method1() throws ClassNotFoundException {
        // Method[]getMethods():                                                返回所有公共成员方法对象的数组，包括继承的
        // 1.获取class对象
        Class<?> clazz = Class.forName("myreflect5.Student");
        // 2.获取成员方法对象并遍历
        for (Method method : clazz.getMethods()) {
            System.out.println(method);
        }
    }
}
